define(function (require, exports, module) {
    require("apps/ebom/page.css");
    require("apps/grid");

    exports.init = function (ctx, uuid, config) {
        var page = new $.PageDomFactory(uuid);
        exports.pageFactory = page;

        var template = '<div class="choose-item" data-item="{{id}}">' +
            '<span class="title">名称/编码：</span>' +
            '{{eName}}/{{code}}</div>';


        function checkItem(row) {
            var box = page.getJqDom("chooseItem");
            if(box.children().length == 1){
                box.children().hide();
            }
            var item = box.find("[data-item=" + row.id + "]");
            if (item.length) {
                return;
            }
            require("plugins/jquery.mustache");
            $(Mustache.render(template, row)).appendTo(box).data("chooseItem", row);
        }

        function unCheckItem(row) {
            var box = page.getJqDom("chooseItem");
            var item = box.find("[data-item=" + row.id + "]");
            if (!item.length) {
                return;
            }
            item.remove();
            if(box.children().length == 1){
                box.children().show();
            }
        }

        exports.getChooseData = function(){
            var results = [];
            page.getJqDom("chooseItem").children(".choose-item").each(function(){
                var data = $(this).data("chooseItem");
                results.push(data);
            });
            return results;
        };

        var pageListComponent = new $.PageListComponent(uuid, {
            view: {
                grid: "grid",
                myToolbar: "news_tb",
                colbar: "user_list_btn"
            },
            configs: {
                gridUrl: ctx + "/app/api/ebom/list?status=PUBLISH&bomType=" + config.bomType + "&tPBom.id=" + config.pbomId,
                idField: "id",
                treeField: "eName",
                gridColumns: [
                    {
                        field: 'id',
                        checkbox: true
                    },
                    {
                        field: 'temp1',
                        hidden: true
                    },
                    {
                        field: 'eName',
                        title: '名称',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'code',
                        title: '编码',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'pBomDes',
                        title: '设计结构',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'bomType',
                        title: '是否装配',
                        width: 100,
                        hidden:true,
                        align: 'left',
                        formatter: function (value, row, index) {
                            return value == "E_BOM_TYPE_UN_BUILD" ? "未装配" : "已装配";
                        }
                    },
                    {
                        field: 'description',
                        title: '功能',
                        width: 100,
                        align: 'left',
                        hidden:true
                    },
                    {
                        field: 'remark',
                        title: '备注',
                        hidden: true,
                        width: 50,
                        align: 'left'
                    },
                    {
                        field: 'cover',
                        title: '封面',
                        width: 100,
                        hidden:true,
                        align: 'left',
                        formatter: function (value, row, index) {
                            return "<img src=\"" + ctx + "/print/" + value + "\" style=\"width:48px;height:32px;\"/>"
                        }
                    },
                    {
                        field: 'createTime',
                        title: '装配时间',
                        width: 100,
                        hidden:true,
                        align: 'left'
                    }
                ],
                onExpand:function(row){
                    if(row.children == undefined){
                        pageListComponent.grid.datagrid("loadChildren", {
                            url:ctx + "/app/api/ebom/children",
                            row:row
                        });
                    }
                },
                onLoadSuccess: $.noop,
                onCheck: function (index, row) {
                    if(!row.bomGroup){
                        checkItem(row);
                    }
                },
                onUncheck: function (index, row) {
                    if(!row.bomGroup){
                        unCheckItem(row);
                    }
                },
                onCheckAll:function(rows){
                    for(var i=0;i<rows.length;i++){
                        if(!rows[i].bomGroup){
                            checkItem(rows[i]);
                        }
                    }
                },
                onUncheckAll:function(rows){
                    for(var i=0;i<rows.length;i++){
                        if(!rows[i].bomGroup){
                            unCheckItem(rows[i]);
                        }
                    }
                }
            },
            events: {
                renderColBarBefore: function (key, row) {
                    return true;
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "view":
                            whenView(context, row);
                            break;
                        default :
                            break;
                    }
                },
                onClickToolbarBtn: function (context, btn) {
                    switch (btn.key) {
                        case "search_all":
                            context.searchAll();
                            break;
                        case "search":
                            whenSearch(context, btn.href);
                            break;
                        default :
                            break;
                    }
                }
            }
        });

        var pageWindow = new $.PageWindow();
        function whenSearch(context, href){
            var that = this;
            pageWindow.create({
                href: href,
                title: "装配结构搜索",
                height: 450,
                width: 600,
                buttonOp: {text: "搜索", iconCls: "e-icon fa fa-search fa-lg"},
                method: "ebom_search",
                callback: function (postData) {
                    page.getJqDom("hiddenTextArea").val(JSON.stringify(postData));
                    context.grid.datagrid("reload", $.utils.buildJson(postData));
                }
            });
            pageWindow.open();
        }

        var globalWindow = new $.PageWindow();

        function whenView(context, row) {
            globalWindow.create({
                href: ctx + "/app/api/ebom/view?id=" + row.id,
                width:800,
                title: row.eName + "的结构查看"
            });
            globalWindow.open();
        }
    }
});